package com.ecelin.utils;

import freemarker.cache.ClassTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;

/**
 * @author ecelin@ecelin.com
 */
public class TemplateUtils {
	public static void convert(String templateName, File path, String fileName, Object object) throws Exception {
		Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
		configuration.setTemplateLoader(new ClassTemplateLoader(ClassLoader.getSystemClassLoader(), "template"));
		configuration.setDefaultEncoding("utf-8");
		configuration.setClassicCompatible(true);
		Template template = configuration.getTemplate(templateName);
		if (!path.exists()) {
			boolean mkdirs = path.mkdirs();
		}
		Writer out = new FileWriter(new File(path, fileName));
		template.process(object, out);
		out.close();
	}
}